<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']"/>
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['icsCustomerId']" />
      </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='姓名'>
          <a-input placeholder='姓名' v-decorator="['name', {rules: [{required: true, message: '请输入姓名'}]}]" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='电话'>
          <a-input placeholder='电话' v-decorator="['mobile', {rules: [{required: true, message: '请输入电话'}]}]" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='用户性别'>
          <a-radio-group @change='onChange' v-decorator="['gender',{rules: [{required: true, message: '请选择性别'}]}]">
            <a-radio :value="'0'">男</a-radio>
            <a-radio :value="'1'">女</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='照片'>
          <a-upload
            list-type='picture-card'
            class='avatar-uploader'
            :show-upload-list='false'
            :action='uploadUrl'
            :headers='headers'
            :before-upload='photoBeforeUpload'
            @change='photoHandleChange'
          >
            <img v-if='photoImageUrl' :src='photoImageUrl' style='width: 102px; height: 102px' />
            <div v-else>
              <a-icon :type="photoLoading ? 'loading' : 'plus'" />
              <div class='ant-upload-text'> 上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='头像'>
          <a-upload
            v-decorator="['avatar']"
            list-type='picture-card'
            class='avatar-uploader'
            :show-upload-list='false'
            :action='uploadUrl'
            :headers='headers'
            :before-upload='avatarBeforeUpload'
            @change='avatarHandleChange'
          >
            <img v-if='avatarImageUrl' :src='avatarImageUrl' style='width: 102px; height: 102px' />
            <div v-else>
              <a-icon :type="avatarLoading ? 'loading' : 'plus'" />
              <div class='ant-upload-text'> 上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='昵称'>
          <a-input placeholder='昵称'  v-decorator="['username', {rules: [{required: true, message: '请输入昵称'}]}]" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='身份证号'>
          <a-input placeholder='身份证号' v-decorator="['cardNo']" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='地址'>
          <a-input placeholder='地址' v-decorator="['address' ,{rules: [{required: true, message: '请输入地址'}]}]" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='邮箱'>
          <a-input placeholder='邮箱' v-decorator="['email']" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='学历'>
          <a-input placeholder='学历' v-decorator="['degree']" />
        </a-form-item>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='紧急联系人'>
          <a-input placeholder='紧急联系人' v-decorator="['urgent']" />
        </a-form-item>

<!--      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">-->
<!--        <a-input placeholder="联系电话" v-decorator="['phone']"/>-->
<!--      </a-form-item>-->
<!--      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">-->
<!--        <a-input placeholder="备注" v-decorator="['remark']"/>-->
<!--      </a-form-item>-->
    </a-form>
  </a-modal>
</template>
<script>
import { importData,getStaffList,updateStaff,getCustomer,getStaffListNotId } from '@/api/admin/customer'
import pick from 'lodash.pick'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
export default {
  name: 'CustomerContactsModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      staffId:'',
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      uploadUrl: process.env.VUE_APP_API_BASE_URL + '/api/dfs/upload',
      imageUrl: '',
      headers: {
        Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN)
      },
      confirmLoading: false,
      mdl: {},
      customerId: undefined,
      contactId: undefined,
      form: this.$form.createForm(this),
      data:[],
      photoImageUrl: '',
      photoLoading: false,
      avatarImageUrl: '',
      avatarLoading: false
    }
  },
  beforeCreate () {
  },
  created () {

  },
  methods: {
    // 照片
    photoBeforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!')
      }
      const isLt2M = file.size / 1024 < 5000
      if (!isLt2M) {
        this.$message.error('图片必须小于 5M!')
      }
      return isJpgOrPng && isLt2M
    },
    photoHandleChange(info) {
      if (info.file.status === 'uploading') {
        this.photoLoading = true
        return
      }
      if (info.file.status === 'done') {
        const { form: { setFieldsValue } } = this
        const result = info.file.response
        // 设置值
        this.$nextTick(() => {
          setFieldsValue({
            // 设置相对路径
            photo: result.fileName
          })
        })
        // Get this url from response in real world.
        this.getBase64(info.file.originFileObj, imageUrl => {
          this.photoImageUrl = imageUrl
          this.photoLoading = false
        })
      }
    },
    // 头像
    avatarBeforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!')
      }
      const isLt2M = file.size / 1024 < 5000
      if (!isLt2M) {
        this.$message.error('图片必须小于 5M!')
      }
      return isJpgOrPng && isLt2M
    },
    avatarHandleChange(info) {
      if (info.file.status === 'uploading') {
        this.avatarLoading = true
        return
      }
      if (info.file.status === 'done') {
        const { form: { setFieldsValue } } = this
        const result = info.file.response
        // 设置值
        this.$nextTick(() => {
          setFieldsValue({
            // 设置相对路径
            avatar: result.fileName
          })
        })
        // Get this url from response in real world.
        this.getBase64(info.file.originFileObj, imageUrl => {
          this.avatarImageUrl = imageUrl
          this.avatarLoading = false
        })
      }
    },
    getBase64(img, callback) {
      const reader = new FileReader()
      reader.addEventListener('load', () => callback(reader.result))
      reader.readAsDataURL(img)
    },
    handleSearch(value){
      let param = {
        mobile:value
      }
      getStaffList(param).then(res =>{
        this.data =res.rows
      })
    },
    handleChange(value){
      console.log(value);
      this.staffId = value;
      // fetch(value, data => (this.data = data));
    },

    add (contactInfo) {
      this.customerId = undefined
      this.customerId = contactInfo.customerId
      this.contactId = undefined
      this.contactId = contactInfo.id
      this.form.resetFields()
      this.edit(contactInfo)
    },
    edit (record) {
      record.icsCustomerId = this.$route.query.customerId
      this.mdl = Object.assign(record)
      console.log(this.mdl.photo)
      if (this.mdl.photo) {
        this.photoImageUrl = process.env.VUE_APP_API_BASE_URL + this.mdl.photo
      }
      if (this.mdl.avatar) {
        this.avatarImageUrl = process.env.VUE_APP_API_BASE_URL + this.mdl.avatar
      }
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id','icsCustomerId','name','username','mobile','gender',
          'photo','avatar','cardNo','address','email','degree','urgent', 'customerId', 'remark'))
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          // values.id = this.staffId
          console.log(this.customerId)
          this.confirmLoading = true
          updateStaff(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
              this.photoImageUrl= ''
              this.avatarImageUrl = ''
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
